home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / External Tool Templates / CPlus Tool Template / Configuration.cp < prev    next >
Encoding:
Text File  |  1998-06-04  |  8.5 KB  |  273 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        Configuration.cp
  3.  *
  4.  *    Contains:    xxx put contents here xxx
  5.  *
  6.  *    Written by:    P. Nagarajan
  7.  *
  8.  *    Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  9.  *
  10.  *    Change History (most recent first):
  11.  *
  12.  *                 3/26/93    NAGA    xxx put comment here xxx
  13.  *
  14.  *    To Do:
  15.  */
  16.  
  17. #ifndef __Configuration__
  18.     #include "Configuration.h"
  19. #endif
  20.  
  21. #ifndef __GESTALTEQU__
  22.     #include <GestaltEqu.h>
  23. #endif
  24.  
  25. #ifndef __PROCESSES__
  26.     #include <Processes.h>
  27. #endif
  28.  
  29. #ifndef __MEMORY__
  30.     #include <Memory.h>
  31. #endif
  32.  
  33. #ifndef __TRAPS__
  34.     #include <traps.h>
  35. #endif
  36.  
  37. #ifndef __OSUTILS__
  38.     #include <OSUtils.h>
  39. #endif
  40.  
  41. #ifndef __THREADS__
  42.     #include <Threads.h>
  43. #endif
  44.  
  45. typedef long *LongIntPtr;
  46.  
  47. //—————————————————————————————————————————————————————————————————————————————————————
  48. //                                Global Variables
  49. //—————————————————————————————————————————————————————————————————————————————————————
  50. extern    Configuration            gConfiguration;
  51. extern    Ptr                        gStrippedAddress;
  52.  
  53.  
  54.  
  55. long StripLong(void * address)
  56. {
  57.     return ((long)address & (long)gStrippedAddress);
  58. } // StripLong 
  59.  
  60. void BlockSet( Ptr destPtr,
  61.                long byteCount,
  62.                unsigned char setVal )
  63. {
  64.     Ptr endPtr = NULL;
  65.     unsigned long longSetVal = 0;
  66.     LongIntPtr longEndPtr = NULL;
  67.     LongIntPtr longSetValPtr;
  68.  
  69.     destPtr = (Ptr)StripLong(destPtr);
  70.     endPtr = (Ptr)(destPtr + byteCount);
  71.  
  72.     //longEndPtr = (LongIntPtr)(destPtr + (byteCount & 0xFFFFFFFC));//Trunc to nearest 4 bytes
  73.     longEndPtr = (LongIntPtr)((long)(destPtr + byteCount) & 0xFFFFFFFC);//Trunc to nearest 4 bytes
  74.  
  75.     //We do longword assignments when we have a chance
  76.     if (byteCount >= 4)
  77.     {
  78.         //if ((((long)destPtr) & 1) == 1)            //Starting on an odd byte boundary
  79.         while (((long)destPtr) & 0x00000003)            //Starting on an odd byte boundary
  80.             *destPtr++ = setVal;
  81.  
  82.         longSetVal = (setVal << 24) + (setVal << 16) + (setVal << 8) + setVal;// Lets get a 4 byte 'punch'.
  83.  
  84.         //Assign in 4 byte chunks what we can
  85.         for (longSetValPtr = (LongIntPtr)destPtr; longSetValPtr < longEndPtr;)
  86.             *longSetValPtr++ = longSetVal;
  87.         destPtr = (Ptr)longSetValPtr;
  88.     }
  89.  
  90.     //Now finish assigning odd bytes
  91.     while (destPtr < endPtr)
  92.         *destPtr++ = setVal;
  93. } // BlockSet 
  94.  
  95. pascal TrapType GetTrapType(short theTrap)
  96.  
  97. {
  98.     // OS traps start with A0, Tool with A8 or AA. 
  99.     if ((theTrap & 0x0800) == 0)                // per D.A.
  100.         return OSTrap;
  101.     else
  102.         return ToolTrap;
  103. } // GetTrapType 
  104.  
  105. //----------------------------------------------------------------------------------------
  106. // NumToolboxTraps: InitGraf is always implemented (trap 0xA86E). If the trap table is
  107. // big enough, trap 0xAA6E will always point to either Unimplemented or some other trap,
  108. // but will never be the same as InitGraf. Thus, you can check the size of the trap table
  109. // by asking if the address of trap 0xA86E is the same as 0xAA6E.
  110. //----------------------------------------------------------------------------------------
  111. #pragma segment Configuration
  112.  
  113. short NumToolboxTraps()
  114. {
  115.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  116.         return 0x200;
  117.     else
  118.         return 0x400;
  119. } // NumToolboxTraps 
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // TrapExists: 
  123. //----------------------------------------------------------------------------------------
  124. #pragma segment Configuration
  125.  
  126. pascal Boolean TrapExists(short theTrap)
  127. {
  128.     TrapType theTrapType = GetTrapType(theTrap);
  129.     short localTrap = theTrap;                    // since theTrap is a const
  130.     if (theTrapType == ToolTrap)
  131.     {
  132.         localTrap = (localTrap & 0x07FF);
  133.         if (localTrap >= NumToolboxTraps())
  134.             localTrap = _Unimplemented;
  135.     }
  136.  
  137.     return NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(localTrap, theTrapType);
  138. } // TrapExists 
  139.  
  140.  
  141.  
  142. //----------------------------------------------------------------------------------------
  143. // HasGestaltAttr: 
  144. //----------------------------------------------------------------------------------------
  145. #pragma segment Configuration
  146.  
  147. Boolean HasGestaltAttr(OSType itsAttr,
  148.                        short itsBit)
  149.  
  150. {
  151.     long response = 0;
  152.  
  153.     return (Gestalt(itsAttr, &response) == noErr) && (((response >> itsBit) & 1) != 0);
  154. } // HasGestaltAttr 
  155.  
  156.  
  157. //----------------------------------------------------------------------------------------
  158. // DefineConfiguration: 
  159. //----------------------------------------------------------------------------------------
  160. #pragma segment Configuration
  161. void DefineConfiguration(Configuration& theConfiguration)
  162. {
  163.     const short mDesktopBus = 0x0400;
  164.  
  165.     OSErr err;
  166.     long response;
  167.  
  168.      // -1 == 0xFFFFFFFF, the largest 32 bit address. Our routine StripLong uses a
  169.     // pre-stripped address gStrippedAddress to avoid the yucky MPW glue.
  170.     gStrippedAddress = StripAddress((Ptr) - 1);
  171.  
  172.     err = Gestalt(gestaltVersion, &response);
  173.     theConfiguration.environsVersion = (short)response;
  174.  
  175.     err = Gestalt(gestaltMachineType, &response);
  176.     theConfiguration.machineType = (short)response;
  177.  
  178.     theConfiguration.hasROM128K = theConfiguration.machineType >= gestaltMac512KE;
  179.     if (theConfiguration.hasROM128K)
  180.         theConfiguration.hasHFS = true; // assume 128K and later ROMs all have HFS
  181.     else
  182.     //    theConfiguration.hasHFS = GetFSFCBLen() > 0;
  183.         theConfiguration.hasHFS = false;
  184.  
  185.     err = Gestalt(gestaltSystemVersion, &response);
  186.     theConfiguration.systemVersion = (short)response;
  187.  
  188.     err = Gestalt(gestaltProcessorType, &response);
  189.     theConfiguration.processor = (short)response;
  190.  
  191.     err = Gestalt(gestaltFPUType, &response);
  192.     theConfiguration.hasFPU = response != gestaltNoFPU;
  193.  
  194.     err = Gestalt(gestaltQuickdrawVersion, &response);
  195.     theConfiguration.hasColorQD = response != gestaltOriginalQD;
  196.  
  197.     err = Gestalt(gestaltQuickdrawVersion, &response);
  198.     theConfiguration.has32BitQD = theConfiguration.hasColorQD && (response != gestalt8BitQD);
  199.  
  200.     err = Gestalt(gestaltKeyboardType, &response);
  201.     theConfiguration.keyboardType = (short)response;
  202.  
  203.     err = Gestalt(gestaltAppleTalkVersion, &response);
  204.     theConfiguration.atDrvrVersNum = (short)response;
  205.  
  206.     theConfiguration.hasSCSI = HasGestaltAttr(gestaltHardwareAttr, gestaltHasSCSI);
  207.  
  208.     err = Gestalt(gestaltAUXVersion, &response);
  209.     theConfiguration.hasAUX = response != 0;
  210.  
  211.     err = Gestalt(gestaltScriptMgrVersion, &response);
  212.     theConfiguration.hasScriptManager = theConfiguration.hasROM128K && (response != 0);
  213.  
  214.     theConfiguration.hasTempMem = HasGestaltAttr(gestaltOSAttr, gestaltTempMemSupport);
  215.  
  216.     err = Gestalt(gestaltTextEditVersion, &response);
  217.     theConfiguration.teVersion = response;
  218.  
  219.     //theConfiguration.hasDesktopBus = (GetHardwareConfigurationFlags() & mDesktopBus) > 0;
  220.  
  221.     theConfiguration.hasHierarchicalMenus = theConfiguration.hasROM128K && TrapExists(_PopUpMenuSelect);
  222.  
  223.     theConfiguration.hasStyleTextEdit = theConfiguration.systemVersion >= 0x600;
  224.     theConfiguration.hasSoundManager = theConfiguration.hasROM128K && TrapExists(_SndDoCommand);
  225.     theConfiguration.hasWaitNextEvent = theConfiguration.hasROM128K && TrapExists(_WaitNextEvent);
  226.  
  227.     // System 7.0 support 
  228.     theConfiguration.hasAppleEventMgr = HasGestaltAttr(gestaltAppleEventsAttr, gestaltAppleEventsPresent);
  229.     theConfiguration.hasAppleEventMgr101 = HasGestaltAttr(gestaltAppleEventsAttr, gestaltScriptingSupport);
  230.     theConfiguration.hasEditionMgr = HasGestaltAttr(gestaltEditionMgrAttr, gestaltEditionMgrPresent);
  231.     theConfiguration.hasHelpMgr = HasGestaltAttr(gestaltHelpMgrAttr, gestaltHelpMgrPresent);
  232.     theConfiguration.hasAliasMgr = HasGestaltAttr(gestaltAliasMgrAttr, gestaltAliasMgrPresent);
  233.     theConfiguration.hasFolderMgr = HasGestaltAttr(gestaltFindFolderAttr, gestaltFindFolderPresent);
  234.     theConfiguration.hasPopupCDEF = HasGestaltAttr(gestaltPopupAttr, gestaltPopupPresent);
  235.     theConfiguration.hasTrueType = HasGestaltAttr(gestaltFontMgrAttr, gestaltOutlineFonts);
  236.  
  237.  
  238.     // ask process mgr for several values
  239.     if (theConfiguration.systemVersion >= 0x700) 
  240.     {
  241.         theConfiguration.hasProcessMgr = HasGestaltAttr(gestaltOSAttr, gestaltLaunchControl);
  242.         theConfiguration.hasThreadMgr = HasGestaltAttr(gestaltThreadMgrAttr, gestaltThreadMgrPresent);
  243.         //theConfiguration.hasThreadMgr = false;
  244.  
  245.         ProcessSerialNumber psn;
  246.         psn.highLongOfPSN = 0;
  247.         psn.lowLongOfPSN = kCurrentProcess;
  248.         
  249.         ProcessInfoRec info;
  250.         info.processInfoLength = sizeof(ProcessInfoRec);
  251.         info.processName = NULL;
  252.         info.processAppSpec = NULL;
  253.  
  254.         if (GetProcessInformation(&psn, &info) == noErr)
  255.         {
  256.             theConfiguration.isOnlyBackground = (info.processMode & modeOnlyBackground) ? true : false;
  257.             theConfiguration.isHighLevelEventAware = (info.processMode & modeHighLevelEventAware) ? true : false;
  258.         }
  259.         else
  260.         {
  261.             theConfiguration.isOnlyBackground = false;
  262.             theConfiguration.isHighLevelEventAware = false;
  263.         }
  264.     }
  265.     else
  266.     {
  267.         theConfiguration.hasProcessMgr = false;
  268.  
  269.         theConfiguration.isOnlyBackground = false;
  270.         theConfiguration.isHighLevelEventAware = false;
  271.     }
  272. } // DefineConfiguration 
  273.